import {
	BASE_URL
} from '../config/index.js'

const config = {
	header: {
		"Content-Type": "application/x-www-form-urlencoded"
	},
	data: {},
	method: 'GET',
	dataType: 'json',
	baseUrl:BASE_URL
}
//同时发送异步代码的次数
let ajaxTimes=0;
const request = (url,method,data={}) => {
	ajaxTimes++;
	wx.showLoading({
    title: '加载中',
  })

	return new Promise((reslove,reject)=>{
		wx.request({
			url: config.baseUrl + url, //仅为示例，并非真实接口地址。
			header: config.header,
			method: method || 'GET',
			data,
			success(res){
				reslove(res.data)
			},
			fail(err){
				reject(err)
				console.log(err);
			},
			complete() {
				ajaxTimes--;
				if(ajaxTimes===0) wx.hideLoading();
			}
		});
	}) 

}
export default request;